package com.simpleframe.validator;

import java.util.Map;

import com.simpleframe.web.ActionMessage;

public abstract class FieldValidator implements Validator{
	
	private ActionMessage actionMessage;
	private Map<String, Object> params;
	
	protected String fieldName;
	protected String label;
	protected String type;
	protected String value;
	
	public void validate(Object object) {
		System.out.println("验证格式");
		String fieldValue = getFieldValue();
		
		if(fieldValue!=null){
			
			if(!fieldValue.matches(getRegEx())){
				addFieldError(fieldName, fieldName + getMessage());
			}
			
		}
		
	}
	
	public String getFieldName(){
		return fieldName;
	}
	
	public String getFieldValue(String fieldName){
		String[] values = (String[])params.get(fieldName);
		if(values!=null){
			return values[0];
		}
		return null;
	}
	
	public String getFieldValue(){
		return getFieldValue(fieldName);
	}
	
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public void setLabel(String label){
		this.label = label;
	}
	
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public String getValue() {
		return value;
	}
	
	public String getMessage(){
		return "";
	}
	
	public String getRegEx(){
		return "";
	}
	
	public void addFieldError(String fieldName, String errorMessage){
		actionMessage.addFieldError(fieldName, errorMessage);
	}
	
	public void setActionMessage(ActionMessage actionMessage) {
		this.actionMessage = actionMessage;
	}
	
}
